package com.hliushi.mq.order;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 顺序消息生成
 *
 * @author llhuang10
 * @date 2021/8/11 20:01
 */
public class OrderProducer {

    public static void main(String[] args) {
        // 1.创建消息生产者producer, 并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        // 2.指定Nameserver地址
        producer.setNamesrvAddr("cvm-node01:9876;cvm-node02:9876");
        // 3.发送消息
        try {
            producer.start();

            String[] tags = new String[]{"TagA", "TagB", "TagC"};

            // 订单列表
            List<OrderStep> orderList = OrderStep.buildOrders();
            Date date = new Date();
            // FastDateFormat fastDateFormat = new FastDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = sdf.format(date);
            for (int i = 0; i < 10; i++) {
                // 加个时间前缀
                String body = dateStr + "hello RocketMq" + orderList.get(i);
                Message message = new Message("TopicTest", tags[i % tags.length], "KEY" + i, body.getBytes());


                /**
                 * public SendResult send(Message msg, MessageQueueSelector selector, Object arg)
                 * msg: 要发送的消息
                 * selector:  消息队列选择器, 通过它我们获取目标消息队列以将消息传递到
                 * arg: 与消息队列选择器一起使用的参数
                 */
                SendResult sendResult = producer.send(message, new MessageQueueSelector() {
                    /**
                     *
                     * @param mqs 消息队列列表
                     * @param msg 发送的每一个消息
                     * @param arg send方法中的那个 arg参数
                     * @return
                     */
                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        System.out.println("mqs = " + mqs);
                        // 根据订单id选择发送queue
                        Long id = (Long) arg;
                        long index = id % mqs.size();
                        return mqs.get((int) index);
                    }
                }, orderList.get(i).getOrderId());

                System.out.printf("SendResult status:%s, queueId:%d, body:%s%n",
                        sendResult.getSendStatus(),
                        sendResult.getMessageQueue().getQueueId(),
                        body);
            }

            // 线程睡一秒
            TimeUnit.SECONDS.sleep(1);
        } catch (MQClientException | InterruptedException | RemotingException | MQBrokerException e) {
            e.printStackTrace();
        } finally {
            // 6.关闭生产者producer
            System.out.println("发送成功!");
            producer.shutdown();
        }
    }
}
